package com.sanji.sjmallmodule.phoneshell;

import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sanji.sjmallapp.MallApplication;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallconfig.ConfigConts;
import com.sanji.sjmallutils.Tools;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 定制订单
 * Created by Administrator on 2017/9/16.
 */

public class ShellOrderAdapter extends BaseAdapter {

    private Context mCtx;
    private List<ShellCartBean> orderList = new ArrayList<>();
    private boolean redo = false;

    private OrderListener orderListener;

    public void setOrderListener(OrderListener listener){
        this.orderListener = listener;
    }

    public ShellOrderAdapter(Context context,List<ShellCartBean> list,boolean isRedo){
        this.mCtx = context;
        this.orderList = list;
        this.redo = isRedo;
    }

    public void notify(List<ShellCartBean> list){
        this.orderList = list;
        this.notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return orderList.size();
    }

    @Override
    public ShellCartBean getItem(int position) {
        return orderList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        OrderHolder holder;
        if (convertView==null){
            convertView = LayoutInflater.from(mCtx).inflate(R.layout.item_shell_order,null);
            holder = new OrderHolder(convertView);
            int imgWidth = MallApplication.myShare.getInt(ConfigConts.ScreenWidth,800) / 3 ;
            holder.kkmoveView.setLayoutParams(new FrameLayout.LayoutParams(imgWidth,imgWidth));
            WebSettings settings = holder.kkmoveView.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setCacheMode(WebSettings.LOAD_DEFAULT);
            holder.kkmoveView.setWebViewClient(new WebViewClient());
            convertView.setTag(holder);
        }else{
            holder = (OrderHolder) convertView.getTag();
        }

        ShellCartBean order = getItem(position);
        if (order!=null){
            holder.orderStatusTv.setText(order.getStatus());
            holder.orderNoTv.setText("订单号:" + order.getOrderNum());
            holder.orderPriceTv.setText(Tools.divide(order.getSalePrice())+"元");
            holder.orderTimeTv.setText("下单时间：" + order.getCreateTime());
            if (redo){
                holder.redoTv.setVisibility(View.VISIBLE);
            }else{
                holder.redoTv.setVisibility(View.GONE);
            }
            holder.kkmoveView.loadUrl(order.getShowView());

            holder.redoTv.setTag(position);
            holder.redoTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = (int) v.getTag();
                    orderListener.kkmoveRedo(pos);
                }
            });

            holder.titleLayout.setTag(position);
            holder.titleLayout.setOnClickListener(itemClickListener);
        }


        return convertView;
    }

    public void updateItemView(ListView listView, int pos, String showUrl){
        int visiblePosition = listView.getFirstVisiblePosition();
        int index = pos - visiblePosition;
        if (index>=0){
            View view = listView.getChildAt(index);
            //得到要更新的item的view
            OrderHolder holder = (OrderHolder) view.getTag();
            //刷新webview
            holder.kkmoveView.loadUrl(showUrl);

        }
    }

    class OrderHolder{

        @BindView(R.id.item_shell_order_title_layout)LinearLayout titleLayout;
        @BindView(R.id.item_shell_order_status_tv)TextView orderStatusTv;
        @BindView(R.id.item_shell_order_no_tv)TextView orderNoTv;
        @BindView(R.id.item_shell_order_price_value_tv)TextView orderPriceTv;
        @BindView(R.id.item_shell_order_time_tv)TextView orderTimeTv;
        @BindView(R.id.item_shell_order_kkmove_web)WebView kkmoveView;
        @BindView(R.id.item_shell_order_kkmove_edit_tv)TextView redoTv;
        public OrderHolder(View view){
            ButterKnife.bind(this,view);
        }
    }

    private View.OnClickListener itemClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int pos = (int) v.getTag();
            orderListener.itemClick(pos);
        }
    };



    interface OrderListener{
        void kkmoveRedo(int pos);
        void itemClick(int pos);
    }

}
